package com.sskj.cndragon.ui.activity;


import android.Manifest;
import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.BasePickerView;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.cndragon.R;
import com.sskj.cndragon.component.DaggerUserDataComponent;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.presenter.UserInfoActivityPresenter;
import com.sskj.cndragon.view.SelectPictureDialog;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import java.io.File;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class UserInfoActivity extends BaseActivity<UserInfoActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_nickname_c)
    TextView tvNicknameC;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_user_id_c)
    TextView tvUserIdC;
    @BindView(R.id.tv_user_id)
    TextView tvUserId;
    @BindView(R.id.tv_shoukuan)
    TextView tvShowKuan;
    @BindView(R.id.tv_address_manager)
    TextView tvAddressManager;
    @Inject
    UserViewModel userViewModel;

    private UserData userData;

    private String mImagePath;
    private SelectPictureDialog selectPictureDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_user_info;
    }

    @Override
    public UserInfoActivityPresenter getPresenter() {
        return new UserInfoActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, tvNicknameC, () -> {
            if (userData != null) {
                NickNameActivity.start(this);
            }
        });
        ClickUtil.click(300, tvAddressManager, () -> {
            if (userData != null) {
                AddressManagerActivity.start(this);
            }
        });
        ClickUtil.click(300, tvShowKuan, () -> {
            if (userData != null) {
                ReceiptActivity.start(this);
            }
        });
        ClickUtil.click(300, ivHead, () -> {
            new RxPermissions(UserInfoActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            selectPic();
                        }
                    });
        });
    }

    @Override
    protected void initData() {
        super.initData();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            if (userData != null) {
                tvNickname.setText(isEmpty(userData.getNickname()) ? "默认用户" : userData.getNickname());
                tvUserId.setText(userData.getUserUid());
                ImageUtil.setCircleImage(userData.getHeadUrl().contains("http") ? userData.getHeadUrl() : HttpConfig.BASE_URL + userData.getHeadUrl(), ivHead);
            }
        });
    }

    public void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(UserInfoActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(UserInfoActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .setOutputCameraPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadFile(new File(mImagePath));
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, UserInfoActivity.class);
        context.startActivity(intent);
    }

    public void changeHead(String headUrl) {
        userData.setHeadUrl(headUrl);
        userViewModel.update(userData);
        ImageUtil.setCircleImage(userData.getHeadUrl().contains("http") ? userData.getHeadUrl() : HttpConfig.BASE_URL + userData.getHeadUrl(), ivHead);
    }
}
